package com.itany.cloud.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

//@Component
public class AuthenticationFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        String company = exchange.getRequest().getQueryParams().getFirst("company");
        if ("itany".equals(company)) {
            // 放行
            return chain.filter(exchange);
        } else {
            // 不放行
            exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
            return exchange.getResponse().setComplete();
        }
    }

    /**
     * 定义过滤器的优先级,返回值越小,优先级越高
     * 相当于web开发中过滤器的load-on-startup配置
     * 取值范围是整个Integer类型的值
     * @return
     */
    @Override
    public int getOrder() {
        return 0;
    }
}
